package com.muyu.system.danmo.controller;

import com.muyu.common.core.enums.PayChannel;
import com.muyu.system.danmo.service.PayService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;

/**
 * 微信回调
 *
 * @author Administrator
 */
@RequestMapping("/open/api/weChatPay/weChatPayCallBack")
@RestController
@Slf4j
@RequiredArgsConstructor
public class WeChatPayCallBackController {

    private final PayService payService;

    /**
     * 微信的的支付回调地址，根据mchCode 区分不同的应用，以获取配置信息
     */
    @PostMapping("/payNotify/{mchCode}/{payChannel}")
    public ResponseEntity<String> payNotify(HttpServletRequest request, @PathVariable String mchCode, @PathVariable PayChannel payChannel) {
        log.info("微信支付回调参数：{}，{}", mchCode, payChannel);
        try {
            return payService.payNotify(request, payChannel, mchCode).contentType(MediaType.APPLICATION_JSON).body("");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON).body("系统异常");
        }
    }



    /**
     * 微信退款回调
     */
    @PostMapping("/refund/payNotify/{mchCode}/{payChannel}")
    public ResponseEntity<String> refundPayNotify(HttpServletRequest request, @PathVariable String mchCode, @PathVariable PayChannel payChannel) {
        log.info("微信退款回调参数：{}，{}", mchCode, payChannel);
        try {
            return payService.refundPayNotify(request, payChannel, mchCode).contentType(MediaType.APPLICATION_JSON).body("");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON).body("系统异常");
        }
    }


}
